/**
 * 
 * @author Sunix
 * @date   2013-5-17
 * @Email  SunixSky@gmail.com
 * 
 */	
package manager{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import sunix.animationPlayer.AnimationPlayerManager;

	public class StageManger{
		
		protected static var mainWindow:Sprite;
		protected static var windowLayer:Sprite;
		protected static var appLayer:Sprite;
		protected static var mask:Sprite;
		
		private static var _stage:Stage;
		
		public static function center(dis:DisplayObject):void{
			dis.x = (stage.stageWidth - dis.width) >> 1;
			dis.y = 50;
		}
		
		public static function init(value:Sprite):void{
			mainWindow = value;
			stage = value.stage;
			createChildLayer();
			createMask();
			value.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected static function onEnterFrame(event:Event):void{
			AnimationPlayerManager.instance.enterFrame();
		}
		
		private static function createMask():void{
			mask = new Sprite();
			mask.graphics.beginFill(0X000000);
			mask.graphics.drawRect(0, 0, 800, 600);
			mask.graphics.endFill();
			mask.alpha = 0.5;
		}
		
		public static function addWindow(dis:DisplayObject):void{
			dis.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveStage);
			windowLayer.addChild(mask);
			windowLayer.addChild(dis);
			center(dis);
		}
		
		protected static function onRemoveStage(event:Event):void{
			var dis:DisplayObject = event.target as DisplayObject;
			dis.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveStage);
			windowLayer.removeChild(mask);
		}
		
		public static function addApp(dis:DisplayObject):void{
			appLayer.addChild(dis);
			center(dis);
		}
		
		public static function get stage():Stage{
			return _stage;
		}

		public static function set stage(value:Stage):void{
			_stage = value;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;
		}
		
		protected static function createChildLayer():void{
			appLayer = new Sprite();
			windowLayer = new Sprite();
			mainWindow.addChild(appLayer);
			mainWindow.addChild(windowLayer);
		}
	}
}